package com.lmk.ms.autoconfigure.doc;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * API文档自动配置
 *
 * @author laomake@hotmail.com
 * @version 1.0
 * @date 2023/1/9
 */
@Configuration
@EnableConfigurationProperties(MsDocProperties.class)
@ConditionalOnProperty(name = "knife4j.enabled", matchIfMissing = true)
public class MsDocAutoconfigure {

    @Autowired
    MsDocProperties msDocProperties;

    /**
     * 根据@Tag 上的排序，写入x-order
     *
     * @return
     */
    @Bean
    public OpenAPI customOpenAPI() {
        Contact contact = new Contact();
        contact.setName(msDocProperties.getConcat());
        contact.setEmail(msDocProperties.getEmail());
        contact.setUrl(msDocProperties.getUrl());

        return new OpenAPI()
                .info(new Info()
                        .title(msDocProperties.getTitle())
                        .contact(contact)
                        .version(msDocProperties.getVersion())
                        .description(msDocProperties.getDescription())
                        .termsOfService(msDocProperties.getUrl())
                )
                .externalDocs(new ExternalDocumentation()
                        .description(msDocProperties.getTitle())
                        .url(msDocProperties.getUrl())
                );
    }


}
